<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\IndexController;
use app\controller\wmapi\PublicCommentController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaAccountingInformation
{
//accountinginformation
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_order_account_information';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;


    /**
     * @param $page
     * @param $pageNum
     * @param string[] $fields
     * @param array $condition
     */
    public static function list($page, $pageNum, $fields = ['*'], $condition = [], $between = [])
    {
        $condition [] = ['is_show','=',1];
        $offset = ($page - 1) * $pageNum;
        if ($between) {
            $model = Db::table('wa_order_account_information')
                ->select($fields)
                ->where($condition)
                ->whereBetween('created_at', $between);
        } else {
            $model = Db::table('wa_order_account_information')
                ->select($fields)
                ->where($condition);
        }


        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();

        if ($list) {
            foreach ($list as $k) {
                if ($k->payment_voucher){
                    $k->payment_voucher = json_decode($k->payment_voucher);
                }
                if ($k->voucher){
                    $k->voucher = json_decode($k->voucher);
                }

                if ($k->status == 1) {
                    $k->status_name = '待确认到账';
                } elseif ($k->status == 2) {
                    $k->status_name = '已到账';
                } elseif ($k->status == 3) {
                    $k->status_name = '异常';
                }  else {
                    $k->status_name = '';
                }

                if ($k->type){
                    $k = IndexController::accountingInformationTypeName($k);
                }

                // 货币类型   1 人民币 2 美元 3 欧元
//                if ($k->currency == 1){
//                    $k->currency_name = '人民币';
//                }elseif ($k->currency == 2){
//                    $k->currency_name = '美元';
//                }elseif ($k->currency == 3){
//                    $k->currency_name = '欧元';
//                }else{
//                    $k->currency_name = '';
//                }
                $k->currency_name = IndexController::currencyNameCommon($k->currency);


            }
        }

        return json_encode(['list' => $list, 'count' => $count]);


    }

    /**
     * 2025/05/26 新账单列表数据
     * @param $page
     * @param $pageNum
     * @param array $fields
     * @param array $condition
     * @param array $between
     * @return false|string
     */
    public static function listByAccInfo($page, $pageNum, $fields = ['*'], $whereIn = [], $condition = [], $between = [])
    {
        $condition [] = ['is_show','=',1];
        $offset = ($page - 1) * $pageNum;
        if ($whereIn){
            if ($between) {
                $model = Db::table('wa_order_account_information')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('admin_id',$whereIn)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_order_account_information')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('admin_id',$whereIn);
            }
        }else{
            if ($between) {
                $model = Db::table('wa_order_account_information')
                    ->select($fields)
                    ->where($condition)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_order_account_information')
                    ->select($fields)
                    ->where($condition);
            }
        }



        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//
        if ($list) {
            foreach ($list as $k) {

                // 查看账单分配状态 allocation_status
                //默认
                $k->allocation_status = 1;
                $k->allocation_status_name = '待分配';
                if ($k->allocation_status){
                    //先判断是否有已审批和待审批的  如果有求总和
                    $fund_type_condition = [];
                    $fund_type_condition [] = ['finance_no','=',$k->finance_no];
                    $fund_type_where_in = [0,1];
                    $transfer_amount = WaAccountingInformationRelevance::sumsBySaesFundType($fund_type_condition, $fund_type_where_in);
                    if ($transfer_amount){
                        PublicCommentController::账单分配状态;
                        if (round($k->total_price,2) == round($transfer_amount,2)){
                            $k->allocation_status = 2;
                            $k->allocation_status_name = '已经足额分配';
                        }elseif (round($k->total_price,2) > round($transfer_amount,2)){
                            if (round($transfer_amount,2) != 0){
                                $k->allocation_status = 3;
                                $k->allocation_status_name = '未足额分配';
                            }
                        }

                    }

                }

                if ($k->payment_voucher){
                    $k->payment_voucher = json_decode($k->payment_voucher);
                }
                if ($k->voucher){
                    $k->voucher = json_decode($k->voucher);
                }

                if ($k->status == 1) {
                    $k->status_name = '待确认到账';
                } elseif ($k->status == 2) {
                    $k->status_name = '已到账';
                } elseif ($k->status == 3) {
                    $k->status_name = '异常';
                }  else {
                    $k->status_name = '';
                }

                if ($k->type){
                    $k = IndexController::accountingInformationTypeName($k);
                }

                $k->currency_name = IndexController::currencyNameCommon($k->currency);


            }
        }

        return json_encode(['list' => $list, 'count' => $count]);


    }


    /**
     * @param $page
     * @param $pageNum
     * @param string[] $fields
     * @param array $condition
     */
    public static function all($fields, $condition)
    {

        $list = Db::table('wa_order_account_information')
                ->select($fields)
                ->where($condition)
                ->get();


        if ($list) {
            foreach ($list as $k) {
                if ($k->payment_voucher){
                    $k->payment_voucher = json_decode($k->payment_voucher);
                }
                if ($k->voucher){
                    $k->voucher = json_decode($k->voucher);
                }

                if ($k->status == 1) {
                    $k->status_name = '待确认到账';
                } elseif ($k->status == 2) {
                    $k->status_name = '已到账';
                } elseif ($k->status == 3) {
                    $k->status_name = '异常';
                }  else {
                    $k->status_name = '';
                }

                // 入账类型 1 定金  2 余款
                if ($k->type == 1) {
                    $k->type_name = '定金';
                } elseif ($k->type == 2) {
                    $k->type_name = '余款';
                }else {
                    $k->type_name = '';
                }


                // 货币类型   1 人民币 2 美元 3 欧元
//                if ($k->currency == 1){
//                    $k->currency_name = '人民币';
//                }elseif ($k->currency == 2){
//                    $k->currency_name = '美元';
//                }elseif ($k->currency == 3){
//                    $k->currency_name = '欧元';
//                }else{
//                    $k->currency_name = '';
//                }
                $k->currency_name = IndexController::currencyNameCommon($k->currency);


            }
        }
        return json_encode($list);


    }



    public static function find($condition)
    {
        $res = Db::table('wa_order_account_information')
            ->select('*')
            ->where($condition)
            ->first();
        if ($res) {
            $k = $res;
            if ($k->payment_voucher){
                $k->payment_voucher = json_decode($k->payment_voucher);
            }
//            $k->order_allocation_data =  $k->wai_approve_log_data;
//            if ($k->order_allocation_data){
//                $k->order_allocation_data = json_decode($k->order_allocation_data);
//            }

            if ($k->voucher){
                $k->voucher = json_decode($k->voucher);
            }

            if ($k->status == 1) {
                $k->status_name = '待确认到账';
            } elseif ($k->status == 2) {
                $k->status_name = '已到账';
            } elseif ($k->status == 3) {
                $k->status_name = '异常';
            }  else {
                $k->status_name = '';
            }

            if ($k->type == 1) {
                $k->type_name = '定金';
            } elseif ($k->type == 2) {
                $k->type_name = '余款';
            } else {
                $k->type_name = '';
            }

            $k->currency_name = IndexController::currencyNameCommon($k->currency);

//
//            $ary_by_finance_relevance_all = []; // 分配的分组数据
//            $ary_by_finance_relevance_no = []; // 获取唯一的 finance_relevance_no  详情要对分配做分组
//            // 获取账单分配销售订单的数据
//            $acc_info_condition = [];
////            $acc_info_condition [] = ['order_sn_parent','=',$k->order_sn_parent];
//            $acc_info_condition [] = ['finance_no','=',$k->finance_no];
////            $acc_info_condition [] = ['status','=',1];
//            $acc_info_data = WaAccountingInformationRelevance::all(['*'],$acc_info_condition);
//            if ($acc_info_data){
//                $acc_info_data = json_decode($acc_info_data);
//                foreach ($acc_info_data as $kai){
//                    if (!in_array($kai->finance_relevance_no,$ary_by_finance_relevance_no)){
//                        array_push($ary_by_finance_relevance_no,$kai->finance_relevance_no);
//                    }
//                }
//                if ($ary_by_finance_relevance_no){
//                    // 获取账单分配销售订单的数据
//                    $fi_rel_condition = [];
//                    $fi_rel_condition [] = ['finance_relevance_no','=',$kai->finance_relevance_no];
//                    $fi_rel_data = WaAccountingInformationRelevance::all(['*'],$acc_info_condition);
//                    if ($fi_rel_data){
//                        $fi_rel_data = json_decode($fi_rel_data);
//
//                        $rel_data = [
//                            'order_allocation_data_all' => $fi_rel_data,
//                        ];
//                        array_push($ary_by_finance_relevance_all,$rel_data);
//                    }
//
//                }
//
//            }
//
//            $k->order_allocation_data_all = $ary_by_finance_relevance_all;
            // 判断账单是否有待审批的订单
            $k->is_wai_approve_status = 0;
            $k->is_wai_approve_status_name = '无待审批订单';
            $acc_info_approve_condition = [];
//            $acc_info_condition [] = ['order_sn_parent','=',$k->order_sn_parent];
            $acc_info_approve_condition [] = ['finance_no','=',$k->finance_no];
            $acc_info_approve_condition [] = ['status','=',0];
            $acc_info_approve_data = WaAccountInformationExamineApprove::findApproveStatusByPrNo($acc_info_approve_condition);
            if ($acc_info_approve_data){
                $acc_info_approve_data = json_decode($acc_info_approve_data);
                if ($acc_info_approve_data){
                    $k->is_wai_approve_status = 1;
                    $k->is_wai_approve_status_name = '有待审批订单';
                }
            }
            $res = $k;
        }
        if ($res){
            $res = json_encode($res);
        }

        return $res;
    }
//

    public static function findApprove($adminId,$condition)
    {
        $res = Db::table('wa_order_account_information')
            ->select('*')
            ->where($condition)
            ->first();
        if ($res) {
            $k = $res;
            if ($k->payment_voucher){
                $k->payment_voucher = json_decode($k->payment_voucher);
            }

            if ($k->voucher){
                $k->voucher = json_decode($k->voucher);
            }

            if ($k->status == 1) {
                $k->status_name = '待确认到账';
            } elseif ($k->status == 2) {
                $k->status_name = '已到账';
            } elseif ($k->status == 3) {
                $k->status_name = '异常';
            }  else {
                $k->status_name = '';
            }

            if ($k->type == 1) {
                $k->type_name = '定金';
            } elseif ($k->type == 2) {
                $k->type_name = '余款';
            } else {
                $k->type_name = '';
            }

            $k->currency_name = IndexController::currencyNameCommon($k->currency);


            $ary_by_finance_relevance_all = []; // 分配的分组数据
            $ary_by_finance_relevance_no = []; // 获取唯一的 finance_relevance_no  详情要对分配做分组
            // 获取账单分配销售订单的数据
            $acc_info_condition = [];
//            $acc_info_condition [] = ['order_sn_parent','=',$k->order_sn_parent];
            $acc_info_condition [] = ['finance_no','=',$k->finance_no];
            $acc_info_condition [] = ['status','<=',2];
            $acc_info_data = WaAccountingInformationRelevance::all(['*'],$acc_info_condition);
            if ($acc_info_data){
                $acc_info_data = json_decode($acc_info_data);
                if ($acc_info_data){
                    foreach ($acc_info_data as $kai){
                        if (!in_array($kai->finance_relevance_no,$ary_by_finance_relevance_no)){
                            array_push($ary_by_finance_relevance_no,$kai->finance_relevance_no);
                        }
                    }
                }

                if ($ary_by_finance_relevance_no){
                    foreach ($ary_by_finance_relevance_no as $kfrn){
                        // 获取账单分配销售订单的数据
                        $fi_rel_condition = [];
                        $fi_rel_condition [] = ['finance_relevance_no','=',$kfrn];
                        $fi_rel_data = WaAccountingInformationRelevance::all(['*'],$fi_rel_condition);
                        if ($fi_rel_data){
                            $fi_rel_data = json_decode($fi_rel_data);
                            $acc_approve_condition = [];
                            $acc_approve_condition [] = ['finance_relevance_no','=',$kfrn];
                            $acc_approve_data_find = WaAccountInformationExamineApprove::findBySalesDetail($adminId,$acc_approve_condition);
                            if ($acc_approve_data_find){
                                $acc_approve_data_find = json_decode($acc_approve_data_find);
                                if ($acc_approve_data_find){
                                    $created_at = $acc_approve_data_find->created_at;
                                }else{
                                    $created_at = '';
                                }

                            }else{
                                $acc_approve_data_find = [];
                                $created_at = '';
                            }


                            $rel_data = [
                                'created_at' => $created_at,
                                'approve_data' => $acc_approve_data_find,
                                'order_allocation_data' => $fi_rel_data,
                            ];
                            array_push($ary_by_finance_relevance_all,$rel_data);
                        }
                    }


                }

            }

            $k->order_allocation_data_all = $ary_by_finance_relevance_all;
            // 判断账单是否有待审批的订单
            $k->is_wai_approve_status = 0;
            $k->is_wai_approve_status_name = '无待审批订单';
            $acc_info_approve_condition = [];
//            $acc_info_condition [] = ['order_sn_parent','=',$k->order_sn_parent];
            $acc_info_approve_condition [] = ['finance_no','=',$k->finance_no];
            $acc_info_approve_condition [] = ['status','=',0];
            $acc_info_approve_data = WaAccountInformationExamineApprove::findApproveStatusByPrNo($acc_info_approve_condition);
            if ($acc_info_approve_data){
                $acc_info_approve_data = json_decode($acc_info_approve_data);
                if ($acc_info_approve_data){
                    $k->is_wai_approve_status = 1;
                    $k->is_wai_approve_status_name = '有待审批订单';
                }
            }

            $res = $k;
        }
        return json_encode($res);
    }


    public static function findApproveByAllocation($adminId,$finance_relevance_no)
    {
        $finance_relevance_condition = [];
        $finance_relevance_condition [] = ['finance_relevance_no','=',$finance_relevance_no];
        $finance_relevance_data = Db::table('wa_order_account_information_relevance')
            ->select('*')
            ->where($finance_relevance_condition)
            ->first();
        if (!$finance_relevance_data){
            return IndexController::errored($finance_relevance_data,'账单分配数据获取失败');
        }

        $acc_if_condition = [];
        $acc_if_condition [] = ['finance_no','=',$finance_relevance_data->finance_no];
        $res = Db::table('wa_order_account_information')
            ->select('*')
            ->where($acc_if_condition)
            ->first();
        if ($res) {
            // 获取账单分配销售订单的数据
            $fi_rel_condition = [];
            $fi_rel_condition [] = ['finance_relevance_no','=',$finance_relevance_no];
            $fi_rel_data = WaAccountingInformationRelevance::all(['*'],$fi_rel_condition);
            if ($fi_rel_data){
                $fi_rel_data = json_decode($fi_rel_data);
                $rel_data = [
                    'approve_data' => json_decode(WaAccountInformationExamineApprove::find($adminId,$fi_rel_condition)),
                    'order_allocation_data' => $fi_rel_data,
                ];
            }
            $res->order_allocation_data_all = $rel_data;
        }


        return json_encode($res);
    }

    public static function created($data)
    {
        return Db::table('wa_order_account_information')->insertGetId($data);
    }

    public static function updated($data, $where)
    {
        $result = Db::table('wa_order_account_information')->where($where)->update($data);
        return $result;
    }



}